/*
 * Copyright (c) 2021 Huize (Chengdu) Network Technology Co., LTD All rights reserved.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Huize Service Inc
 * All Rights Reserved @2018
 */

package com.huize.zhike.framework.common.enumeration;

import org.apache.commons.lang3.StringUtils;

import java.util.stream.Stream;


/**
 * 描述: 业务码枚举类
 *
 * @author tianyuliang
 * @version $Id: ErrorEnum.java, v0.1 2021/2/4
 */
public enum ErrorEnum {

    SUCCESS("00000", "成功"),

    UNKNOWN_ERROR("99999", "未知异常"),

    INVALID_PARAMS("30001", "参数错误或参数无效"),

    NULL_POINTER_ERROR("30002", "空指针异常"),

    DATA_CONVERSION_EXCEPTION("30003", "数据转换异常"),

    DATA_CONVERTER_NULL_OBJECT("30004", "分页获取参数dataConverter为空"),

    LIST_PARAM_INVALID("30005", "list类型参数不能为空"),

    DATE_TYPE_INVALID("30014", "日期类型参数无效!"),

    LIST_ITEM_PARAM_INVALID("30006", "List类型节点items参数不能为空"),

    PARAM_CACHE_KEY_INVALID("30007", "缓存Key为空、或无效"),

    PARAM_AUTH_KEY_INVALID("30007", "API接口授权参数auth值无效"),

    SERIAL_ID_PARAM_INVALID("30027", "主键ID参数无效"),

    LOGIN_USER_INVALID("30010", "登陆用户名不存在、cookie已过期、未登录"),

    PARSE_JWT_TOKEN_INVALID("30050", "json反序列化token失败"),

    PARSE_JWT_TOKEN_ENCODING_NULL("30053", "token解码失败"),

    CLIENT_ERROR("30060", "查询接口触发4XX异常"),

    SERVER_ERROR("30061", "查询接口触发5XX异常"),

    CLIENT_NETWORK_ERROR("30062", "网络异常,例如ping不通或telnet失败"),

    ;


    private final String code;

    private final String desc;

    ErrorEnum(String code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public static ErrorEnum of(String code) {
        if (StringUtils.isEmpty(code)) {
            return INVALID_PARAMS;
        }

        return Stream.of(values()).filter(b -> b.getCode().equalsIgnoreCase(code.trim())).findFirst().orElseGet(() -> INVALID_PARAMS);
    }


    public String getCode() {
        return code;
    }


    public String getDesc() {
        return desc;
    }

    @Override
    public String toString() {
        return String.format("[%s,%s]", getCode(), getDesc());
    }


}
